package thread;

import java.util.Random;
import java.util.Scanner;

public class Game24 {
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        int[] card = new int[7];
        while (true) {
            setCard(card);
            System.out.println("是否要挑战y/n");
            String ans = scan.next();
            if(ans.equals("y")){
                break;
            }
        }
        for (int i = 0;i<3;i++) {
            int result = cal(card);
            if(result!=24){
                card[4+i] = result;
                printCard(card);
            }else{
                System.out.println("恭喜你赢了!");
                break;
            }
        }
        System.out.println("挑战失败!");
    }
    //计算
    private static int cal(int[] card) {
        int c1,c2,result = 0;
        String key ;
        while (true){
            System.out.println("请输入第一个数");
            c1 = scan.nextInt();
            if(isContain(card,c1)){
                break;
            }
        }
        while (true) {
            System.out.println("请输入运算符");
            key = scan.next();
            if(key.equals("+")||key.equals("-")||key.equals("*")||key.equals("/")){
                break;
            }
        }
        while (true){
            System.out.println("请输入第二个数");
            c2 = scan.nextInt();
            if(isContain(card,c2)){
                break;
            }
        }
        switch (key){
            case "+" :{
                result = c1 + c2;
                break;
            }
            case "-" :{
                result = c1 - c2;
                break;
            }
            case "*" :{
                result = c1 * c2;
                break;
            }
            case "/" :{
                result = c1 / c2;
                break;
            }
        }
        return result;
    }
    //判断数字是否存在
    private static boolean isContain(int[] card, int num) {
        for(int i = 0;i<6;i++){
            if(card[i]==num){
                card[i]=0;
                return true;
            }
        }
        System.out.println("该数不存在，请检查");
        return false;
    }
    //发牌
    public static void setCard(int[] card){
//        System.out.println("牌1\t"+"牌2\t"+"牌3\t"+"牌4\t"+"中1\t"+"中2\t"+"中3\t");
        Random random = new Random();
        for(int i = 0;i<4;i++){
            int tmp = random.nextInt(13)+1;
            card[i] = tmp;
        }
//        for(int n :card){
//            System.out.print(n+"\t");
//        }
        printCard(card);
    }
    //显示牌
    public static void printCard(int[] card){
        System.out.println("牌1\t"+"牌2\t"+"牌3\t"+"牌4\t"+"中1\t"+"中2\t"+"中3\t");
        for(int n :card){
            System.out.print(n+"\t");
        }
        System.out.println();
    }
}
